package com.mpale.soa.cookie;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 值写入Cookie
 * @author lijian
 * @date 2020/6/15
 */
public class CookieWriteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        Cookie cookie = new Cookie("name", "李健");
        int version = cookie.getVersion();
        String path = cookie.getPath();
        String domain = cookie.getDomain();

        // Cookie响应客服端
        resp.addCookie(cookie);

        PrintWriter writer = resp.getWriter();
        writer.println("版本号：" + version);
        writer.println("路径：" + path);
        writer.println("作用域：" + domain);
        writer.println("Cookie中的Key：" + cookie.getName());
        writer.println("Cookie中的Value：" + cookie.getValue());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
